package com.mengxingyun.shortlink.admin.toolkit;

import java.util.Random;

/**
 * @Description: 分组ID随机生成器
 * @Author: heshen-mengxingyun
 * @Date: 2024/4/23 11:52
 */
public final class RandomGenerator {
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    /**
     * 生成随机分组ID
     * @param length 生成的位数
     * @return
     */
    public static String generateRandom(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }

        return sb.toString();
    }

    /**
     * 生成六位的随机ID
     * @return
     */
    public static String generateRandom() {
        return generateRandom(6);
    }
}
